package com.baitu.websokettest.controller;

import com.baitu.websokettest.entity.Message;
import com.baitu.websokettest.entity.User;
import com.baitu.websokettest.service.MessageService;
import com.baitu.websokettest.service.UserService;
import com.baitu.websokettest.util.MailTool;
import com.baitu.websokettest.util.ResultJson;
import com.baitu.websokettest.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;
import java.util.Random;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    /**
     * 登录
     *
     * @param user    用户
     * @param Session 会话
     * @return {@link ResultJson}
     */
    @RequestMapping("/login")
    public ResultJson login(User user, HttpSession Session) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        User re = this.userService.getOne(wrapper);
        Session.setAttribute("user", re);
        return ResultJson.ok(re);
    }

    /**
     * 获取验证码
     *
     * @param email   电子邮件
     * @param request 请求
     * @return boolean
     */
    @RequestMapping("/getCode")
    @ResponseBody
    public boolean getCode(String email, HttpServletRequest request) {
        //随机生成一个验证码
        Integer code = new Random().nextInt(900000) + 99999;
        request.getSession().setAttribute(email + "code", code.toString());
        request.getSession().setAttribute("email", email);
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        MailTool.send(email, code);
                    }
                }
        ).start();
        return true;
    }

    /**
     * @param user    用户
     * @param session 会话
     * @return {@link ResultJson}
     */
    @RequestMapping("/zhuce")
    public ResultJson zhuCe(UserVO user, HttpSession session) {
        String oldCode = (String) session.getAttribute(user.getUserEmail() + "code");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_email", user.getUserEmail()).or().eq("username", user.getUsername());
        List<User> count = this.userService.list(wrapper);
        if (count.size() == 0) {
            if (oldCode.equals(user.getCode())) {
                return ResultJson.r(this.userService.save(user));
            } else {
                return ResultJson.error("验证码错误！");
            }
        } else {
            return ResultJson.error("邮箱已经注册或用户名重复！");
        }
    }

    /**
     * 检查登录
     *
     * @param session 会话
     * @return {@link ResultJson}
     */
    @RequestMapping("/checklogin")
    public ResultJson checkLogin(HttpSession session) {
        if (session.getAttribute("user") != null) {
            return ResultJson.ok(session.getAttribute("user"));
        }
        return ResultJson.error();
    }

    /**
     * 记录
     *
     * @param mess
     * @param session 会话
     * @return {@link List}<{@link Message}>
     */
    @RequestMapping("/record")
    public List<Message> record(Message mess, HttpSession session) {
        List<Message> messageList = (List<Message>) session.getAttribute(mess.getToId() + "");
        if (Objects.isNull(messageList)) {
            List<Message> messageList1 = this.messageService.selectLog(mess);
            session.setAttribute(mess.getToId() + "", messageList1);
            return messageList1;
        } else {
            return messageList;
        }
    }
}
